package com.probiz.estore.imports.handler.product;

import org.apache.log4j.Logger;

import com.probiz.estore.common.model.product.Product;
import com.probiz.estore.imports.handler.PersistenceHandler;
import com.probiz.estore.imports.model.ImportModel;
import com.probiz.estore.product.service.ProdMainManager;
import com.probiz.estore.product.service.ProductManager;

public class ProductPersistenceHandlerImpl implements PersistenceHandler{
	private Logger logger=Logger.getLogger(ProductPersistenceHandlerImpl.class);
	private ProductManager productManager;
	private ProdMainManager prodMainManager;
	public void setProductManager(ProductManager productManager) {
		this.productManager = productManager;
	}
	public void setProdMainManager(ProdMainManager prodMainManager) {
		this.prodMainManager = prodMainManager;
	}
	@SuppressWarnings("unchecked")
	public void saveOrUpdate(ImportModel importModel) {
		Product product = (Product)importModel.getTarget();
		if(!importModel.isUpdate()){
			//case:新增
			if(null ==product.getProductType()){
				//设置结果
				importModel.setResult(ImportModel.RESULT_ERROR);
				String msg = "无法导入，新增商品必须指定【商品类型】";
				logger.error(msg);
				importModel.setMsg(msg);
				return;
			}
		} else {
			product.setCreateBy(importModel.getAppUserId());
		}
		product.setUpdateBy(importModel.getAppUserId());
		
		
		prodMainManager.saveFullyForImport(product);
	}
	
	public void validate(ImportModel importModel) {
	}


	
}
